package com.example.weatherforecast;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.example.weatherforecast.citypagetool.ManageActivity_ButtonAdapter;
import com.example.weatherforecast.citypagetool.SwipeToDelete;
import com.hxc.basemodule.BaseActivity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
 * @Author：coderChen
 * @Project：WeatherForecast
 * Explain:显示添加的城市，可删除
 */

public class ManageActivity extends BaseActivity {
    private RecyclerView recyclerView;
    private ManageActivity_ButtonAdapter adapter;
    private List<String> cityList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage);
        setImmersiveWindows();

        // 返回上一页
        Toolbar toolbar = findViewById(R.id.manageTitle);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        cityList = new ArrayList<>();
        // 从 SharedPreferences 中读取城市数据
        SharedPreferences sharedPreferences = getSharedPreferences("CityData", Context.MODE_PRIVATE);
        Set<String> citySet = sharedPreferences.getStringSet("cities", new HashSet<>());
        Log.d("CityData", "读取到的城市数据：" + citySet.toString());
        cityList = new ArrayList<>(citySet);

        // 初始化 RecyclerView 和 Adapter
        recyclerView = findViewById(R.id.cityManage);
        adapter = new ManageActivity_ButtonAdapter(cityList, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        // 左滑删除指定城市
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new SwipeToDelete(adapter,sharedPreferences));
        itemTouchHelper.attachToRecyclerView(recyclerView);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_manage;
    }

    public void btnAddCityClick(View view){
        Intent intent = new Intent(ManageActivity.this,AddCityActivity.class);
        startActivity(intent);
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK ) {
            //do something.
            return true;
        } else {
            return super.dispatchKeyEvent(event);
        }
    }
//    下级页面返回该页面时更新城市列表
    @Override
    protected void onResume() {
        super.onResume();
        // 重新加载 SharedPreferences 中的城市数据
        SharedPreferences sharedPreferences = getSharedPreferences("CityData", Context.MODE_PRIVATE);
        Set<String> citySet = sharedPreferences.getStringSet("cities", new HashSet<>());
        Log.d("CityData", "读取到的城市数据：" + citySet.toString());
        cityList.clear(); // 清空当前列表
        cityList.addAll(citySet); // 更新列表数据
        adapter.notifyDataSetChanged(); // 通知 RecyclerView 更新数据
    }

}